home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / GETYN.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  3KB  |  79 lines

  1. /* +++Date last modified: 26-Oct-1996 */
  2.  
  3. /************************************************************************/
  4. /*                                                                      */
  5. /*  GETYN.C                                                             */
  6. /*                                                                      */
  7. /*  Original Copyright 1993-94 by Robert B. Stout as part of            */
  8. /*  the MicroFirm Function Library (MFL)                                */
  9. /*                                                                      */
  10. /*  The user is granted a free limited license to use this source file  */
  11. /*  to create royalty-free programs, subject to the terms of the        */
  12. /*  license restrictions specified in the LICENSE.MFL file.             */
  13. /*                                                                      */
  14. /************************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include "sniptype.h"
  19. #include "snipkbio.h"
  20.  
  21. /************************************************************************/
  22. /*                                                                      */
  23. /*  getYN()                                                             */
  24. /*                                                                      */
  25. /*  Prompt a user for a Yes/No response with default and timeout        */
  26. /*  features.                                                           */
  27. /*                                                                      */
  28. /*  Parameters: 1 - Prompt string                                       */
  29. /*              2 - Default response, 'Y' or 'N', '\0' for none         */
  30. /*              3 - Timeout before default assumed - 0 for none         */
  31. /*                                                                      */
  32. /*  Returns: True_ or False_                                            */
  33. /*                                                                      */
  34. /*  NOTE: All output is via stderr                                      */
  35. /*                                                                      */
  36. /************************************************************************/
  37.  
  38. Boolean_T getYN(char *prompt, int def_ch, unsigned timeout)
  39. {
  40.       int ch;
  41.       char *yn = " (y/n) ";
  42.       clock_t start, limit = (clock_t)0;
  43.  
  44.       fputs(prompt, stderr);
  45.       
  46.       def_ch = toupper(def_ch);
  47.       if ('Y' == def_ch)
  48.             yn[2] = def_ch;
  49.       else if ('N' == def_ch)
  50.             yn[4] = def_ch;
  51.       else  def_ch = '\0';
  52.  
  53.       fputs(yn, stderr);
  54.  
  55.       if (0 != def_ch && 0 < timeout)
  56.       {
  57.             start = clock();
  58.             limit = (clock_t)(CLK_TCK * timeout);
  59.       }
  60.       while ('Y' != ch && 'N' != ch)
  61.       {
  62.             while (!kbhit())
  63.             {
  64.                   if (limit && (limit <= (clock() - start)))
  65.                   {
  66.                         ch = def_ch;
  67.                         goto BYE;
  68.                   }
  69.             }
  70.             ch = toupper(getch());
  71.             if (def_ch && 'Y' != ch && 'N' != ch)
  72.                   ch = def_ch;
  73.       }
  74.  
  75. BYE:  fputc(ch, stderr);
  76.       fputc('\n', stderr);
  77.       return ('Y' == ch);
  78. }
  79.